/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OperaLibro.java
 *
 * Created on 10/02/2012, 01:19:10 PM
 */
package vista;

import control.controlLibro;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import negocio.Libro;

/**
 *
 * @author Luis Felipe Gomez
 */
public class OperaLibro extends javax.swing.JFrame {

    private Libro j;
    private int i, temp;
    private controlLibro controL;
    Principal p;
    ArrayList list;

    public OperaLibro() {
        initComponents();
        j = new Libro();
        controL = new controlLibro();
        i = 1;
        temp = 0;
        p = new Principal();
        list = new <Libro> ArrayList();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        isbnLibro = new javax.swing.JTextField();
        tituloLibro = new javax.swing.JTextField();
        nuevoLibro = new javax.swing.JToggleButton();
        modificarLibro = new javax.swing.JToggleButton();
        eliminarLibro = new javax.swing.JToggleButton();
        buscarLibro = new javax.swing.JToggleButton();
        RegresarLibro = new javax.swing.JButton();
        reservadoLibro = new javax.swing.JCheckBox();
        prestadoLibro = new javax.swing.JCheckBox();
        todosLibros = new javax.swing.JButton();
        adelateAlterno = new javax.swing.JButton();
        devolverAleatorio = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Isbn");

        jLabel2.setText("Tiltulo");

        nuevoLibro.setText("nuevo");
        nuevoLibro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevoLibroActionPerformed(evt);
            }
        });

        modificarLibro.setText("modificar");
        modificarLibro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarLibroActionPerformed(evt);
            }
        });

        eliminarLibro.setText("Eliminar");
        eliminarLibro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarLibroActionPerformed(evt);
            }
        });

        buscarLibro.setText("Buscar");
        buscarLibro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarLibroActionPerformed(evt);
            }
        });

        RegresarLibro.setText("Regresar al menu");
        RegresarLibro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegresarLibroActionPerformed(evt);
            }
        });

        reservadoLibro.setText("reservado");

        prestadoLibro.setText("prestado");

        todosLibros.setText("Mostrar Todos");
        todosLibros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                todosLibrosActionPerformed(evt);
            }
        });

        adelateAlterno.setText(">>");
        adelateAlterno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adelateAlternoActionPerformed(evt);
            }
        });

        devolverAleatorio.setText("<< ");
        devolverAleatorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                devolverAleatorioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RegresarLibro, javax.swing.GroupLayout.DEFAULT_SIZE, 896, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(58, 58, 58))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(devolverAleatorio, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(33, 33, 33)
                                        .addComponent(nuevoLibro))
                                    .addComponent(isbnLibro, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(13, 13, 13)
                                        .addComponent(modificarLibro)
                                        .addGap(83, 83, 83)
                                        .addComponent(todosLibros))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(110, 110, 110)
                                        .addComponent(buscarLibro, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(reservadoLibro)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(204, 204, 204)
                                .addComponent(eliminarLibro)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(adelateAlterno, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(167, 167, 167)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(prestadoLibro)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(4, 4, 4)
                                        .addComponent(tituloLibro, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tituloLibro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(isbnLibro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscarLibro))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(prestadoLibro)
                            .addComponent(reservadoLibro))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(eliminarLibro)
                            .addComponent(nuevoLibro)
                            .addComponent(devolverAleatorio)
                            .addComponent(modificarLibro)
                            .addComponent(todosLibros))
                        .addGap(13, 13, 13))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(adelateAlterno)
                        .addGap(18, 18, 18)))
                .addComponent(RegresarLibro)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void buscarLibroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarLibroActionPerformed

    if (buscarLibro.isSelected()) {
        tituloLibro.setText("");
        isbnLibro.setText("");
        tituloLibro.setEnabled(false);
        buscarLibro.setLabel("Ejecutar");
        tituloLibro.setText("");
        isbnLibro.setText(null);
        prestadoLibro.setSelected(false);
        reservadoLibro.setSelected(false);

    } else {
        buscarLibro.setLabel("Buscar");
        if (isbnLibro.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, " no se puede realizar la busqueda");


        } else {

            j = this.controL.bucarLibro(Integer.parseInt(isbnLibro.getText()));
            tituloLibro.setEnabled(true);
            tituloLibro.setText(j.getTitulo());
            isbnLibro.setText("" + j.getIsbn());
            if (j.getPrestamo() == 1) {
                prestadoLibro.setSelected(true);

            }
            if (j.getReservado() == 1) {
                reservadoLibro.setSelected(true);
            }
        }

    }



}//GEN-LAST:event_buscarLibroActionPerformed

private void todosLibrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_todosLibrosActionPerformed

    Alllibros all = new Alllibros();
    all.setVisible(true);
    setVisible(false);


}//GEN-LAST:event_todosLibrosActionPerformed

private void nuevoLibroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevoLibroActionPerformed
    if (nuevoLibro.isSelected()) {
        tituloLibro.setText("");
        isbnLibro.setText("");
        nuevoLibro.setLabel("Ejecutar");
        prestadoLibro.setEnabled(false);
        reservadoLibro.setEnabled(false);

    } else {
        nuevoLibro.setLabel("Ejecutar");
        if (tituloLibro.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, " no se puede realizar la opreación");


        } else {

            if (isbnLibro.getText().isEmpty()) {
                j.setTitulo(tituloLibro.getText());
                if (prestadoLibro.isSelected()) {
                    int i = 1;
                    j.setPrestamo(i);
                }
                if (reservadoLibro.isSelected()) {
                    int i = 1;
                    j.setReservado(i);
                }
                JOptionPane.showMessageDialog(null, "se ha realizado la acción con exito ");

                this.controL.crearLibro(j);
                tituloLibro.setText("");
            } else {

                j.setTitulo(tituloLibro.getText());
                j.setIsbn(Integer.parseInt(isbnLibro.getText()));
                JOptionPane.showMessageDialog(null, "se ha realizado la acción con exito ");

                this.controL.crearLibroIsbn(j);
                tituloLibro.setText("");
                isbnLibro.setText("");

            }

        }
    }

}//GEN-LAST:event_nuevoLibroActionPerformed

private void eliminarLibroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarLibroActionPerformed
    if (eliminarLibro.isSelected()) {
        eliminarLibro.setLabel("ejecutar");
        prestadoLibro.setEnabled(false);
        reservadoLibro.setEnabled(false);
        tituloLibro.setEnabled(false);
        isbnLibro.setText("");
        tituloLibro.setText("");
    } else {
        eliminarLibro.setLabel("eliminar");
        if (isbnLibro.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "no se puede realizar la acción de elimonación");
        } else {
            if (reservadoLibro.isSelected() || prestadoLibro.isSelected()) {
                JOptionPane.showMessageDialog(null, "no se puede eliminar pqrque el libro esta en prestado o reservado");
            } else {
                controL.eliminarLibro(Integer.parseInt(isbnLibro.getText()));
            }
        }
    }

}//GEN-LAST:event_eliminarLibroActionPerformed

private void modificarLibroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarLibroActionPerformed
    if (modificarLibro.isSelected()) {
        modificarLibro.setLabel("ejecutar");
        prestadoLibro.setEnabled(false);
        reservadoLibro.setEnabled(false);

    } else {
        modificarLibro.setLabel("modificar");
        if (tituloLibro.getText().isEmpty() || isbnLibro.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "No se puede realizar la la modificación el campo título o el isbn esta vacio  ");
        } else {
            controL.modificarTitulo(Integer.parseInt(isbnLibro.getText()), tituloLibro.getText());
            JOptionPane.showMessageDialog(null, "la modificación ha sido exitoda ");
        }

    }

    // TODO add your handling code here:
}//GEN-LAST:event_modificarLibroActionPerformed

private void RegresarLibroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegresarLibroActionPerformed
    p.setVisible(true);
    setVisible(false);



}//GEN-LAST:event_RegresarLibroActionPerformed

private void adelateAlternoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adelateAlternoActionPerformed






    try {

        ResultSet p = controL.todos();
        list.clear();
        reservadoLibro.setSelected(false);
        prestadoLibro.setSelected(false);
        while (p.next() != false) {

            Libro u = new Libro(p.getInt(1), p.getString(2), p.getInt(3), p.getInt(4));
            list.add(u);
            System.out.println("" + p.getString(2));
        }
        System.out.println("" + list.size());
        if (isbnLibro.getText().isEmpty() || tituloLibro.getText().isEmpty()) {

            Libro y = (Libro) list.get(temp);

            isbnLibro.setText("" + y.getIsbn());
            tituloLibro.setText("" + y.getTitulo());
            if (y.getPrestamo() == 1) {
                prestadoLibro.setSelected(true);

            }
            if (y.getReservado() == 1) {
                reservadoLibro.setSelected(true);

            }

        } else {
            temp++;
            if (temp < list.size()) {

                Libro y = (Libro) list.get(temp);
                System.out.println("" + temp);
                isbnLibro.setText("" + y.getIsbn());
                tituloLibro.setText(y.getTitulo());
                if (y.getPrestamo() == 1) {
                    prestadoLibro.setSelected(true);

                }
                if (y.getReservado() == 1) {
                    reservadoLibro.setSelected(true);

                }
            }
            if (temp >= list.size()) {

                JOptionPane.showMessageDialog(null, "no hay mas libros");
                temp--;
            }



        }

    } catch (SQLException ex) {
        Logger.getLogger(OperaLibro.class.getName()).log(Level.SEVERE, null, ex);
    }




}//GEN-LAST:event_adelateAlternoActionPerformed

private void devolverAleatorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_devolverAleatorioActionPerformed
    ResultSet p = controL.todos();
    list.clear();
    reservadoLibro.setSelected(false);
    prestadoLibro.setSelected(false);
    try {

        while (p.next() != false) {
            Libro n = new Libro(p.getInt(1), p.getString(2), p.getInt(3), p.getInt(4));
            list.add(n);
        }

        if (tituloLibro.getText().isEmpty() && isbnLibro.getText().isEmpty()) {
            j = (Libro) list.get(temp);
            tituloLibro.setText(j.getTitulo());
            isbnLibro.setText("" + j.getIsbn());
            if (j.getPrestamo() == 1) {
                prestadoLibro.setSelected(true);

            }
            if (j.getReservado() == 1) {
                reservadoLibro.setSelected(true);

            }

        } else {
            temp--;
            if (temp >= 0) {

                j = (Libro) list.get(temp);
                tituloLibro.setText(j.getTitulo());
                isbnLibro.setText("" + j.getIsbn());
                if (j.getPrestamo() == 1) {
                    prestadoLibro.setSelected(true);

                }
                if (j.getReservado() == 1) {
                    reservadoLibro.setSelected(true);

                }
            } else {
                JOptionPane.showMessageDialog(null, "no hay mas registros");
                temp++;
            }
        }
    } catch (SQLException ex) {
        Logger.getLogger(OperaLibro.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_devolverAleatorioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OperaLibro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OperaLibro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OperaLibro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OperaLibro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new OperaLibro().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton RegresarLibro;
    private javax.swing.JButton adelateAlterno;
    private javax.swing.JToggleButton buscarLibro;
    private javax.swing.JButton devolverAleatorio;
    private javax.swing.JToggleButton eliminarLibro;
    private javax.swing.JTextField isbnLibro;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JToggleButton modificarLibro;
    private javax.swing.JToggleButton nuevoLibro;
    private javax.swing.JCheckBox prestadoLibro;
    private javax.swing.JCheckBox reservadoLibro;
    private javax.swing.JTextField tituloLibro;
    private javax.swing.JButton todosLibros;
    // End of variables declaration//GEN-END:variables
}
